#/usr/bin/env python

import os, pygame, math
from pygame.locals import *
import hex_map, right_click 
from pyscape_settings import *

#-----------------------------------
# Initialization
#-----------------------------------
pygame.init()    
#screen = pygame.display.set_mode((500,350))
screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)

tile_dir = os.path.abspath( 'resources' )
map = hex_map.hex_map( screen=screen, tile_dir=tile_dir )
box = right_click.right_click()

#-----------------------------------
# Setup some edges
#-----------------------------------
factor = 1.75
thick = 64
edge = thick / 3
width = screen.get_width()
height = screen.get_height()

s_bot = pygame.Surface( (width, thick), flags=SRCALPHA)
s_right = pygame.Surface( (thick, height), flags=SRCALPHA)

for i in range( thick ):
    pygame.draw.line(s_bot, (255, 255, 255, min(255, int(i**factor))), (0, i), (width, i))
    pygame.draw.line(s_right, (255, 255, 255, min(255, int(i**factor))), (i,0), (i,width))

s_top = pygame.transform.rotate(s_bot, 180)
s_left = pygame.transform.rotate(s_right, 180)

def draw_border():
    screen.blit(s_top, (0, 0))
    screen.blit(s_left, (0, 0))
    screen.blit(s_bot, (0, height-s_bot.get_height()))
    screen.blit(s_right, (width-s_right.get_width(), 0))

#-----------------------------------
# Setup right Click boxes
#-----------------------------------

pos_tile = (0, 0)
pos_mouse = (-1, -1)
scale = 0

def make_view_changer( x ):
    def view_change():
        global pos_tile
        pos_tile = map.pixel_to_tile( pos_mouse[0], pos_mouse[1] )
        global scale 
        scale = x
    return view_change

def reset():
    global pos_tile
    pos_tile = (0, 0)
    global scale 
    scale = 0

lbl_en = [ 0, 0, 0]
lbl_txt = { "Zoom 14":make_view_changer(14), "Zoom 27":make_view_changer(27), "Reset":reset }

lbl_set_en = [0]*len(map._tiles)
lbl_set_txt = map._tiles.keys()


lbl_height_txt = [ '1','2','3','4','5','6']
lbl_height_en = [0]*6


fnt = pygame.font.Font(pygame.font.get_default_font(),12)
elev = False

while 1:
    pygame.time.wait(10)
    screen.fill(( 0,0,0 ))
    map.draw_map(pos_tile, scale, elev)
    draw_border()
    map.set_cursor(pos_mouse[0], pos_mouse[1])
        
    # Write text in legend
    text = fnt.render( map.text, 0, (0x0f,0x0f,0x0f))
    screen.blit( text, (edge, screen.get_height() - edge))
    pygame.display.flip()
                                                                 
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
            elif event.key == K_SPACE:
                print "Cursor: " + str(map.temp_hex)
                #print "Mouse: " + str(pos_mouse)
                #print "Tile: " + str( map.pixel_to_tile(pos_mouse[0], pos_mouse[1] ) )
                map.set_tile( pos_mouse[0], pos_mouse[1], box.right_click( pos_mouse, screen, lbl_set_txt, lbl_set_en ))
            elif event.key == K_e:
                elev = ~elev
            elif event.key == K_h:
                map.set_tile_height( pos_mouse[0], pos_mouse[1], int(box.right_click( pos_mouse, screen, lbl_height_txt, lbl_height_en )))
        
        elif event.type == MOUSEMOTION:
            pos_mouse = event.pos
        elif event.type == MOUSEBUTTONUP and event.button == 3:
            ret = box.right_click( event.pos, screen, lbl_txt.keys(), lbl_en )
            for lbl in lbl_txt.keys():
                if ret == lbl:
                    lbl_txt[ lbl ]()


